'''
Created on Oct 20, 2011

@author: oabalbin
'''

'''
Created on Nov 30, 2010

@author: oabalbin
'''
import os
#import subprocess

def fixmateInformation(indexed_bam_file, outfile, use_mem, path_to_picard, temp_dir):
    '''
    #MAX_RECORDS_IN_RAM ?
    '''
    picard_command=path_to_picard+'FixMateInformation.jar'
    use_temp_dir='-Djava.io.tmpdir='+temp_dir
    #outfile=indexed_bam_file.replace('.bam','.fixMate.bam')
    #maxinram=500000
    
    args = ['java','-Xmx'+str(use_mem)+'m', use_temp_dir,'-jar',picard_command,
            'I='+indexed_bam_file,'SO=coordinate', 'VALIDATION_STRINGENCY=SILENT',
            'O='+outfile
            ] 
    
    #args= [a.replace(',',';') for a in args]
    #command = ",".join(args).replace(',',' ').replace(';',',')

    return args


def markDuplicates(indexed_bam_file, outfile, use_mem, path_to_picard):
    '''
    '''
    picard_command=path_to_picard+'MarkDuplicates.jar'
    #outfile=indexed_bam_file.replace('.bam','.markdup.bam')
    metrics_file=indexed_bam_file.replace('.bam','.duplicates.stats')
    
    args=['java','-Xmx'+str(use_mem)+'m','-jar', picard_command, 
          'I='+indexed_bam_file, 'O='+outfile, 'METRICS_FILE='+metrics_file,
          'ASSUME_SORTED=true','VALIDATION_STRINGENCY=SILENT'
          ]
    
    #args= [a.replace(',',';') for a in args]
    #command = ",".join(args).replace(',',' ').replace(';',',')

    return args


def sortSam(unsorted_bam_file, use_mem, path_to_picard):
    '''
    '''
    sorted_bam_file=unsorted_bam_file.replace('.bam','.psorted.bam')
    picard_command=path_to_picard+'SortSam.jar'
    args=['java','-Xmx'+str(use_mem)+'m','-jar', picard_command, 
          'I='+unsorted_bam_file, 'O='+sorted_bam_file,
          'SORT_ORDER=coordinate',
          'VALIDATION_STRINGENCY=SILENT'
          ]
    
    #args= [a.replace(',',';') for a in args]
    #command = ",".join(args).replace(',',' ').replace(';',',')

    return args,sorted_bam_file

def buildIndex(sorted_bam_file, use_mem, path_to_picard):
    '''
    BuildBamIndex 
    '''
    index_bam_file=sorted_bam_file.replace('.bam','.bam.bai')
    picard_command=path_to_picard+'BuildBamIndex.jar'
    args=['java','-Xmx'+str(use_mem)+'m','-jar', picard_command, 
          'I='+sorted_bam_file, 'O='+index_bam_file,
          'VALIDATION_STRINGENCY=SILENT'
          ]
    
    #args= [a.replace(',',';') for a in args]
    #command = ",".join(args).replace(',',' ').replace(';',',')

    return args,sorted_bam_file

def sortIndexSam(input_sam_file, output_bam_file, use_mem=2048, picard_path=None, sort_order='coordinate'):
    '''
    sort sam or bam
    SO=SortOrder Sort order of output file  Required. Possible values: {unsorted, queryname, coordinate} 
    '''
    picard_command = os.path.join(picard_path,"SortSam.jar")

    args = ['java', '-Xmx'+str(use_mem)+'m', '-jar', picard_command, 
            'I=%s' % (input_sam_file), 
            'O=%s' % (output_bam_file),
            'SORT_ORDER=%s' % (sort_order),
            'CREATE_INDEX=true',
            'VALIDATION_STRINGENCY=SILENT']
    
    #args= [a.replace(',',';') for a in args]
    #command = ",".join(args).replace(',',' ').replace(';',',')

    return args

def picard_mergebam(list_bam_files, output_bam_file, use_mem=2048, picard_path=None, sort_order='coordinate'):
    '''
    '''
    input_files=[]
    for bamfile in list_bam_files:
        input_files += ['I=%s' % (bamfile)]

    picard_command = os.path.join(picard_path, "MergeSamFiles.jar")
    args1 = ['java', '-Xmx'+str(use_mem)+'m', '-jar', picard_command]

    args2=  ['O=%s' % (output_bam_file),
            'SORT_ORDER=%s' % (sort_order),
            'CREATE_INDEX=true',
            'MERGE_SEQUENCE_DICTIONARIES=true',
            'VALIDATION_STRINGENCY=SILENT']
    
    args=args1+input_files+args2
#    args= [a.replace(',',';') for a in args]
#    command = ",".join(args).replace(',',' ').replace(';',',')

    return args
